package com.itheima.test;

import com.alibaba.fastjson.JSON;
import com.itheima.pojo.User;
import com.itheima.service.UserService;
import org.apache.http.HttpHost;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.indices.CreateIndexRequest;
import org.elasticsearch.client.indices.CreateIndexResponse;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.xcontent.XContentType;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.io.IOException;

public class elasticDemo {
    private RestHighLevelClient restHighLevelClient;
    @Before
    public void init() throws IOException {
        restHighLevelClient = new RestHighLevelClient(
                RestClient.builder(
                        new HttpHost("localhost", 9200, "http"),
                        new HttpHost("localhost", 9201, "http"),
                        new HttpHost("localhost", 9202, "http")
                )
        );
    }

    @After
    public void close() throws IOException {
        restHighLevelClient.close();
    }

    @Test
    public void testCreateIndex() throws IOException{
        // 创建CreateIdexRequest对象，并指定索引库名称
        CreateIndexRequest request = new CreateIndexRequest("user");
//        设置settings配置
        request.settings(Settings.builder()
                .put("index.number_of_shards", 3)
                .put("index.number_of_replicas", 1));
        // 3.指定mapping配置
        request.mapping(
                "{\n" +
                        "    \"properties\": {\n" +
                        "      \"id\": {\n" +
                        "        \"type\": \"long\"\n" +
                        "      },\n" +
                        "      \"name\":{\n" +
                        "        \"type\": \"keyword\"\n" +
                        "      },\n" +
                        "      \"age\":{\n" +
                        "        \"type\": \"integer\"\n" +
                        "      },\n" +
                        "      \"gender\":{\n" +
                        "        \"type\": \"keyword\"\n" +
                        "      },\n" +
                        "      \"note\":{\n" +
                        "        \"type\": \"text\",\n" +
                        "        \"analyzer\": \"ik_max_word\"\n" +
                        "      }\n" +
                        "    }\n" +
                        "  }",
                XContentType.JSON);
        CreateIndexResponse response = restHighLevelClient.indices().create(request, RequestOptions.DEFAULT);
        System.out.println("response = " + response.isAcknowledged());


    }

    private UserService userService = new UserService();

    @Test
    public void addDocument() throws IOException{
//        从数据库查询文档数据
        User user = userService.findById(6l);

//        创建indexrequest对象，并指定索引库名称
        IndexRequest request = new IndexRequest("user");
//        指定新增的的数据id。这里的id一定要用string
        request.id(user.getId().toString());
//        将新增的文档数据变成json格式
        String jsonString = JSON.toJSONString(user);
//        将json数据添加到indexRequest中
        request.source(jsonString, XContentType.JSON);
//        发起请求，得到结果
        IndexResponse indexResponse = restHighLevelClient.index(request, RequestOptions.DEFAULT);
        System.out.println("======index====" + indexResponse.getResult());

    }


}
